// patrolnpc.txt
// Creature attacks anything it hates nearby, and spends the rest of its time patrolling a path.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - Number of path to follow.
//	 Cell 1 - Which group am I? 0 or 1
//   Cell 2 - Node if talked to. 0 means no conversation
//   Cell 3,4 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

variables;

short i,target;
short trav = 0;

body;

beginstate INIT_STATE;
	set_boss_level(ME,1);
	set_new_abil(ME,20);
	set_level(ME,28);
	set_strategy(ME,10);
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE;
	if (get_foe_target(ME,6,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	if ((gf(79,10 + get_memory_cell(1)) == 2) && (char_ok(44)) && (dist_to_char(44) <= 16) && (get_target() != 44)) {
		set_foe_target(ME,44);
		set_state(3);
		}
	if ((gf(79,10 + get_memory_cell(1)) == 2) && (char_ok(45)) && (dist_to_char(45) <= 16) && (get_target() != 45)) {
		set_foe_target(ME,45);
		set_state(3);
		}
	if ((gf(79,10 + get_memory_cell(1)) == 2) && (char_ok(46)) && (dist_to_char(46) <= 16) && (get_target() != 46)) {
		set_foe_target(ME,46);
		set_state(3);
		}
	if ((gf(79,10 + get_memory_cell(1)) == 2) && (char_ok(47)) && (dist_to_char(47) <= 16) && (get_target() != 47)) {
		set_foe_target(ME,47);
		set_state(3);
		}
		
	if (gf(79,10 + get_memory_cell(1)) < 2) {
		if (my_dist_from_start() >= 6) {
			if (get_ran(1,1,100) < 40) 
				return_to_start(ME,1);
			}
			else fidget(ME,25);
		}
	if ((gf(79,10 + get_memory_cell(1)) == 2) && (trav == 0)) {
		set_act_at_dist(ME,1);
		follow_path(ME,get_memory_cell(1),0);
		set_state(4);
		}
	if (((gf(79,10 + get_memory_cell(1)) == 2) && (trav > 0)) ||
	  (gf(79,10 + get_memory_cell(1)) == 3)) {
		if (dist_to_nav_point(ME,1 + get_memory_cell(1)) > 6) 
			approach_nav_point(ME,1 + get_memory_cell(1),1);
			else fidget(ME,20);
		}
		
	if (gf(79,10 + get_memory_cell(1)) == 4) {
		if (dist_to_nav_point(ME,3 + get_memory_cell(1)) > 4) 
			approach_nav_point(ME,3 + get_memory_cell(1),1);
			else fidget(ME,10);
		}
		

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);



	do_attack();
break;

beginstate 4; // patrol

	if (dist_to_nav_point(ME,1 + get_memory_cell(1)) <= 8) {
		trav = 1;
		set_state(START_STATE);
		}
		
	if (follow_path(ME,get_memory_cell(1),0) == TRUE)
		set_state(START_STATE);
break;

beginstate TALKING_STATE;
	if (gf(79,10 + get_memory_cell(1)) < 2) {
		sf(79,12,10 + get_memory_cell(1));
		begin_talk_mode(50);
		}
	if ((gf(79,10 + get_memory_cell(1)) == 2) && (gf(79,7) < 2)) {
		begin_talk_mode(15);
		}
	if (((gf(79,10 + get_memory_cell(1)) == 2) || (gf(79,10 + get_memory_cell(1)) == 3)) && (gf(79,7) >= 2)) {
		sf(79,12,10 + get_memory_cell(1));
		begin_talk_mode(60);
		}
	if (gf(79,10 + get_memory_cell(1)) == 4) {
		begin_talk_mode(15);
		}
break;